package com.neuronrobotics.opengl.scene;

import java.util.Arrays;

import com.neuronrobotics.replicator.gui.stl.GeneralTransform3D.MatrixOrder;

public abstract class SceneNode {
	
	GroupNode parentNode = null;
	boolean isVisible = true;
	private float[] modelTransform;
	private MatrixOrder theMatrixOrder;
	
	/**
	 * Checks if this node has a parent or not
	 * @return true if parent is not null
	 */
	public boolean hasParent(){
		return parentNode != null;
	}
	
	/**
	 * Sets newParent as the parentNode, removing connections to current parent
	 * if necessary
	 * @param newParent
	 * @return
	 */
	protected abstract boolean setParent(GroupNode newParent);

	/**
	 * Returns visibility of this node
	 */
	public boolean isVisible(){
		return this.isVisible;
	}
	
	/**
	 * Sets visibility of this node
	 * @param newVisibility
	 */
	public void setVisible(boolean newVisibility){
		this.isVisible = newVisibility;
	}
		
	protected boolean setTransform(float[] newTransform, MatrixOrder matrixOrder) {
		if(newTransform.length<16) return false;
		modelTransform = Arrays.copyOf(newTransform, 16);
		theMatrixOrder = matrixOrder;
		return true;
	}

	protected boolean getTransform(float[] resultTransform, MatrixOrder matrixOrder) {
		if(resultTransform.length<16) return false;
		for(int i=0;i<16;i++) resultTransform[i] = modelTransform[i];
		if(this.theMatrixOrder!=matrixOrder){
			MatrixOrder.convertOrder(resultTransform, 0, resultTransform, 0);
		}		
		return true;
	}
	
	/**
	 * This handles the actual drawing of the node and it's children
	 * @param viewMatrix
	 * @param projMatrix
	 */
	protected abstract void draw(float[] viewMatrix, float[] projMatrix);

	public static final void draw(GroupNode rootNode, float[] mVMatrix,
			float[] mProjMatrix) {
		rootNode.draw(mVMatrix, mProjMatrix);		
	}
	
	
}
